home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / GraphicsCards / StormMesa / OpenStep / Old_Demos / generic.m < prev    next >
Text File  |  1998-12-15  |  3KB  |  111 lines

  1. /* generic.m*/
  2.  
  3. /* Demo of OPENSTEP Mesa rendering */
  4.  
  5. /*
  6.  * See Mesa/include/GL/osmesa.h for documentation of the OSMesa functions.
  7.  *
  8.  * If you want to render BIG images you'll probably have to increase
  9.  * MAX_WIDTH and MAX_HEIGHT in src/config.h.
  10.  *
  11.  * This program is in the public domain.
  12.  *
  13.  * Brian Paul
  14.  *
  15.  * NEXTSTEP output provided by Pascal Thibaudeau 
  16.  * pthibaud@frbdx11.cribx1.u-bordeaux.fr
  17.  * OpenStep conversion by Pete French
  18.  * pete@ohm.york.ac.uk
  19.  */
  20.  
  21. #import <AppKit/AppKit.h>
  22. #import <stdio.h>
  23. #import <stdlib.h>
  24. #import "GL/osmesa.h"
  25.  
  26. extern int gl_width,gl_height;
  27. extern void render_image(void);
  28.  
  29. int main( int argc, char *argv[] )
  30. {
  31.    OSMesaContext ctx;
  32.    unsigned char *buffer;
  33.    NSWindow *myWindow;
  34.    NSView *myView;
  35.    NSMenu *myMenu;
  36.    NSBitmapImageRep *bitmap;
  37.  
  38.    unsigned long start, end;
  39.    char name[50];
  40.    NSRect GR;
  41.    NSPoint position;
  42.      
  43.    [[NSAutoreleasePool alloc] init];
  44.    NSApp=[NSApplication sharedApplication];
  45.  
  46.    /* Create an RGBA-mode context */
  47.    ctx = OSMesaCreateContext( GL_RGBA, NULL );
  48.  
  49.    /* Allocate the image buffer */
  50.    buffer = malloc( gl_width * gl_height * 4 );
  51.    
  52.    /* Bind the buffer to the context and make it current */
  53.    OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, gl_width, gl_height );
  54.    OSMesaPixelStore( OSMESA_Y_UP, 0 );
  55.  
  56.    bitmap = [[ NSBitmapImageRep alloc] initWithBitmapDataPlanes:&buffer
  57.                                        pixelsWide:gl_width
  58.                                        pixelsHigh:gl_height
  59.                                        bitsPerSample:8 samplesPerPixel:4
  60.                                        hasAlpha:YES isPlanar:NO
  61.                                        colorSpaceName:NSDeviceRGBColorSpace
  62.                                        bytesPerRow:0 bitsPerPixel:0];
  63.   GR = NSMakeRect(100, 100, gl_width, gl_height);
  64.  
  65.   myWindow = [[ NSWindow alloc] initWithContentRect:GR
  66.                                 styleMask:NSTitledWindowMask|
  67.                                           NSMiniaturizableWindowMask
  68.                                 backing:NSBackingStoreBuffered defer:NO];
  69.  
  70.   sprintf(name, "Mesa demo: `%s'", argv[0]);
  71.  
  72.    myView = [[ NSView alloc] initWithFrame:GR];
  73.  
  74.    myMenu = [[ NSMenu alloc] initWithTitle:@"OpenStep Mesa"];
  75.    [myMenu addItemWithTitle:@"Quit"
  76.            action:@selector(terminate:)
  77.            keyEquivalent:@"q"];
  78.    [myMenu sizeToFit];
  79.  
  80.    [myWindow setTitle:[NSString stringWithCString:name]];
  81.    [myWindow display];
  82.    [myWindow setContentView:myView];
  83.    [myWindow makeKeyAndOrderFront:nil];
  84.  
  85.    [NSApp setMainMenu:myMenu];
  86.  
  87.    [myView lockFocus];
  88.  
  89.    /* here is the Mesa call */
  90.    start=time(0);
  91.    render_image();
  92.    end=time(0);
  93.    printf("Rendering took %ld seconds\n",end-start);
  94.    fflush(stdout);
  95.  
  96.    /* draw the bitmap */
  97.    [bitmap draw];
  98.    [bitmap release];
  99.    [myWindow flushWindow];
  100.    [myView unlockFocus];
  101.    free( buffer );
  102.  
  103.    /* destroy the context */
  104.    OSMesaDestroyContext( ctx );
  105.  
  106.    [NSApp run];
  107.    [NSApp release];
  108.  
  109.    return 0;
  110. }
  111.